<?php

/**
 * MVM_MALL 网上商店系统  商品销售分析
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-10-06 $
 * $Id: sale_goods.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if($action=='list'){
    switch ($per){
        case 'cart':
            {
               $result = $db->query("SELECT SUM(a.buy_number) as total_num,a.goods_name,a.order_id,a.goods_id,a.buy_price,b.uid,b.addtime,b.status,c.uid,c.goods_stock,c.goods_cost,c.goods_file1 FROM `{$tablepre}order_goods` a INNER JOIN `{$tablepre}order_info` b ON a.order_id=b.uid INNER JOIN `{$tablepre}goods_table` c ON a.goods_id=c.uid WHERE b.status>1 AND b.status<8 GROUP BY a.goods_id  ORDER BY total_num DESC");
                while($rt = $db->fetch_array($result)) {
                    $co_num++;
                    $rt['total_sale'] = currency($rt['buy_price']*$rt['total_num']);
                    $rt['url']        = GetBaseUrl('product',$rt['goods_id']);
                    $rt['num']        = $co_num;
                    $cart_rt[]        = $rt;
                }
               require_once template('sale_goods');
                footer();
                break;
            }
        case 'look':
            {
                $look_list= array();
                $result = $db->query("SELECT goods_hit,goods_name,uid,goods_file1 FROM `{$tablepre}goods_table`  GROUP BY goods_hit DESC LIMIT 0,30");
                while($rt = $db->fetch_array($result)) {
                    $co_num++;
                    $rt['url'] = GetBaseUrl('product',$rt['uid']);
                    $rt['num'] = $co_num;
                    $look_rt[] = $rt;
                }
                require_once template('sale_goods');
                footer();
                break;
            }
        default:
            {
                $start_time = $start_time ? time_format($start_time):time_format(date('Y-m-j'));
                $arr        = $end_time?explode('-',$end_time):explode('-',date('Y-m-j'));
                $end_time   = mktime(23,59,59,$arr[1],$arr[2],$arr[0]);
                $result     = $db->query("SELECT SUM(a.buy_number) as total_num,a.goods_name,a.order_id,a.goods_id,a.buy_price,b.uid,b.addtime,b.status,c.uid,c.goods_stock,c.goods_cost FROM `{$tablepre}order_goods` a INNER JOIN `{$tablepre}order_info` b ON a.order_id=b.uid INNER JOIN `{$tablepre}goods_table` c ON a.goods_id=c.uid WHERE b.addtime>'$start_time' AND b.addtime<'$end_time' AND b.status>1 AND b.status<8 GROUP BY a.goods_id  ORDER BY total_num DESC");
                while($rt = $db->fetch_array($result)) {
                    $co_num++;
                    $rt['toal_cost']  = $rt['goods_cost']*$rt['total_num'];
                    $rt['total_sale'] = $rt['buy_price']*$rt['total_num'];
                    $rt['price']      = $rt['total_sale']- $rt['toal_cost']; 
                    $rt['url']        = GetBaseUrl('product',$rt['goods_id']);
                    $total_cost      += $rt['toal_cost'];
                    $total_sale      += $rt['total_sale'];
                    $rt['num']        = $co_num;
                    $sale_rt[]        = $rt;
                }
                $start_time  = date('Y-m-j',$start_time);
                $end_time    = date('Y-m-j',$end_time);
                $total_price = currency(($total_sale-$total_cost));
                $total_sale  = currency($total_sale);
                $total_cost  = currency($total_cost);
                require_once template('sale_goods');
                footer();
            }
    }
} else {
    admin_msg('pass_worng');
}
